using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using newsApi.Models;
using newsApi.Services;
using static newsApi.Services.UsersService;

namespace newsApi.Controllers;
    [ApiController]
    [Route("[controller]")]
    public class UsersController : ControllerBase
    {
        private IUserService _userService;

        public UsersController(IUserService userService)
        {
            _userService = userService;
        }

        [HttpPost]
        public IActionResult Authenticate(AuthenticateRequest model)
        {
            Console.WriteLine("req");
            Console.Write(model.Username);
            var response = _userService.Authenticate(model);

            if (response == null)
                return BadRequest(new { message = "Username or password is incorrect" });

            return Ok(response);
        }

        [Authorize]
        [HttpGet]
        public IActionResult GetAll()
        {
            var users = _userService.GetAll();
            return Ok(users);
        }
    }